package be.kuleuven.thesis.pages;

import java.util.ArrayList;

import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.Window;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener;
import be.kuleuven.thesis.R;

public class GenerecsTabActivity extends TabActivity {
	public static final int TAB_HEIGHT = 50;
	private ArrayList<String> history;
	private OnTabChangeListener onTabChangeListener;
	public static GenerecsTabActivity tabActivity;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.generecs_tab);
        tabActivity = this;
        history = new ArrayList<String>();
        
        Resources res = getResources();
        addTab(Search.class, "Search", res.getDrawable(R.drawable.search));
        addTab(NowPlaying.class, "Now Playing", res.getDrawable(R.drawable.play));
        addTab(CurrentPlaylistGroup.class, "Current Playlist", res.getDrawable(R.drawable.playlist));
        
        TabHost tabHost = getTabHost();
        history.add("Search");
        tabHost.setCurrentTab(0);
        
        tabHost.setOnTabChangedListener(new OnTabChangeListener() {
			
			@Override
			public void onTabChanged(String tabId) {
//				for(int i = 0; i < history.size(); i++)
//					System.out.println(i+": "+history.get(i));
				if(history.get(history.size()-1) != tabId)
					history.add(tabId);
				
				if(GenerecsTabActivity.this.onTabChangeListener != null)
					GenerecsTabActivity.this.onTabChangeListener.onTabChanged(tabId);
			}
		});
        
//        int childCount = tabHost.getTabWidget().getChildCount();
//	    for(int i=0; i<childCount; i++)
//	    	tabHost.getTabWidget().getChildAt(i).getLayoutParams().height = TAB_HEIGHT;
    }
    
    public void switchTab(int tab){
    	TabHost tabHost = getTabHost();
    	tabHost.setCurrentTab(tab);
    }
    
    private void addTab(Class<? extends Activity> pagina, String title,  Drawable icon) {
	    TabHost tabHost = getTabHost();  // The activity TabHost
	    TabHost.TabSpec spec;  // Resusable TabSpec for each tab
	    Intent intent;  // Reusable Intent for each tab

	    // Create an Intent to launch an Activity for the tab (to be reused)
	    intent = new Intent().setClass(this, pagina);

	    // Initialize a TabSpec for each tab and add it to the TabHost
	    spec = tabHost.newTabSpec(title).setIndicator(title, icon)
	                  .setContent(intent);
	    tabHost.addTab(spec);
	}
    
    public void back() {
		if(history.size() > 1) {
			history.remove(history.size()-1);
			TabHost tabHost = getTabHost();
			tabHost.setCurrentTabByTag(history.get(history.size()-1));
		}else {
			finish();
		}
	}

	public void setOnTabChangedListener(OnTabChangeListener onTabChangeListener) {
		this.onTabChangeListener = onTabChangeListener;
	}
}